package com.ucomm.talent.manager;

import org.apache.http.client.HttpResponseException;

import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.vita.bduck.BDuckHttp;
import com.vita.bduck.http.AjaxCallBack;
import com.vita.bduck.http.AjaxParams;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * 
 * @author Administrator
 * @Description:提交数据到网络
 * @date:2013-7-3
 *
 */
public class PostDataToNet {
	private Context context;
	private Handler handler;
	
	public PostDataToNet(Context context, Handler handler) {
		this.context = context;
		this.handler = handler;
	}
	
	/**
	 * 上传数据到网络
	 */
	@SuppressWarnings("unchecked")
	public void postDataToNetwork (final String url, AjaxParams params){
		BDuckHttp fh = new BDuckHttp();
		fh.post(url, params, new AjaxCallBack() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				Message msg = new Message();
                msg.what = AppConstants.MsgStatus.MSG_LOADING;
                handler.sendMessage(msg);
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Object t) {
				if (t instanceof String) {
					String data = (String) t;
					if(url == AppConstants.LOGIN){
						Message msg = new Message();
						
						
						msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
						handler.sendMessage(msg);
					}
					
				}
				
				
			}
			
			@Override
			public void onFailure(Throwable t, String strMsg) {
				// TODO Auto-generated method stub
				Log.i("vita", ">>>>>>>>....<<<<<<<<"
                        + strMsg);
                Message msg = new Message();
                msg.what = AppConstants.MsgStatus.MSG_ERROR;
                if(t instanceof  HttpResponseException){
                    msg.obj = AppException.http((HttpResponseException)t);
                    handler.sendMessage(msg);
                }else{
                    msg.obj = AppException.http(new Exception("strMsg"));
                    handler.sendMessage(msg);
                }
			}
		});
	}
}
